package com.web.admin.web.base.open;

import com.blue.core.dict.HttpMethod;
import com.blue.core.dict.annotation.DictParser;
import com.blue.http.annotation.Http;
import com.blue.http.message.HttpRequest;
import com.blue.http.message.View;
import com.blue.validation.ValidationUtil;
import com.blue.validation.group.SaveModel;
import com.blue.validation.group.UpdateModel;
import com.web.base.open.facade.RoleFacade;
import com.web.base.open.message.RoleRequest;
import com.web.base.open.message.RoleResponse;
import com.web.base.open.message.RoleUrlRequest;
import com.web.core.util.Const;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

@Controller("openRoleController")
@Http(url = "/base/open")
public class RoleController
{
	@Autowired
	private RoleFacade roleFacade;

	private static DictParser dictParser = DictParser.getInstance();
	
	public RoleController()
	{
	}
	
	@Http(url = "/role_list", method = HttpMethod.GET)
	public View listGet(HttpRequest request)
	{
		RoleRequest roleRequest = request.getQueryObject(RoleRequest.class);
		return View.createBuilder().setView("/base/open/role_list")
				.put("model", request)
				.put("page", roleFacade.listPage(roleRequest, Const.PAGE_START, Const.PAGE_SIZE))
				.put("map", dictParser.getStringMap())
				.build();
	}
	
	@Http(url = "/role_list", method = HttpMethod.POST)
	public View listPost(HttpRequest request)
	{
		RoleRequest roleRequest = request.getPostObject(RoleRequest.class);
		int start = request.getPostInt("start", Const.PAGE_START);
		return View.createBuilder().setView("/base/open/role_list_grid")
				.put("page", roleFacade.listPage(roleRequest, start, Const.PAGE_SIZE))
				.build();
	}
	
	@Http(url = "/role_get", method = HttpMethod.GET)
	public View get(HttpRequest request)
	{
		int id = request.getQueryInt("id", 0);
		RoleResponse response = null;
		if (id > 0)
		{
			response = roleFacade.get(id);
		}
		return View.createBuilder().setView("/base/open/role_get")
				.put("model", response == null ? new RoleResponse() : response)
				.put("map", dictParser.getStringMap())
				.build();
	}
	
	@Http(url = "/role_save", method = HttpMethod.POST)
	public void save(HttpRequest request)
	{
		RoleRequest roleRequest = request.getPostObject(RoleRequest.class);
		RoleResponse response = null;
		if (roleRequest.getId() == null)
		{
			ValidationUtil.valid(roleRequest, SaveModel.class);
			response = roleFacade.add(roleRequest);
		}
		else
		{
			ValidationUtil.valid(roleRequest, UpdateModel.class);
			response = roleFacade.update(roleRequest);
		}
	}
	
	@Http(url = "/role_url_get", method = HttpMethod.GET)
	public View getUrl(HttpRequest request)
	{
		int id = request.getQueryInt("id", 0);
		return View.createBuilder().setView("/base/open/role_url_get")
				.put("model", roleFacade.get(id))
				.put("treeList", roleFacade.listSelect(id))
				.build();
	}
	
	@Http(url = "/role_url_save", method = HttpMethod.POST)
	public void saveUrl(HttpRequest request)
	{
		RoleUrlRequest roleUrlRequest = request.getPostObject(RoleUrlRequest.class, UpdateModel.class);
		roleFacade.saveRoleUrl(roleUrlRequest);
	}
	
	@Http(url = "/role_url_refresh", method = HttpMethod.POST)
	public void refresh(HttpRequest request)
	{
		roleFacade.refresh();
	}
	
}
